package com.awifi.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.awifi.demo.entity.User;
import com.awifi.demo.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.internal.Function;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @description: ToDo
 * @author: marin
 * @time: 2020/8/3 10:53
 */
@Slf4j
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("/get")
    public String getString(String name) {
        //String value= (String) redisTemplate.opsForValue().get(name);
        Object value = redisUtil.get(name);
        System.out.println(value);
        return JSON.toJSONString(value);
    }

   /* @PostMapping("/lpush")
    public Long lpush(String name,Object object) {
        return redisTemplate.opsForList().leftPush(name,object);
    }*/

    @GetMapping("/set")
    public void set(String name,String value) {
        redisTemplate.opsForValue().set(name,value);
        System.out.println(redisTemplate.opsForValue().get(name));
    }

    @PutMapping("/setUser")
    public void setUser(User user) {
        user.setName("wangwu");
        user.setAge(20);
        redisUtil.lpush(user.getName(), JSON.toJSONString(user));
    }

    @GetMapping("/getUser")
    public String getUser() {
        ArrayList list = new ArrayList();
       // list = (ArrayList) redisUtil.getUser("wangwu");
        System.out.println(list);
        return "hello";
    }

    @PostMapping("/time")
    public String timeExpire() {
        redisUtil.set("ccut","changchun", (long) 60);
        Object tju = redisUtil.get("ccut");
        System.out.println(tju);
        return JSON.toJSONString(tju);
    }

    @GetMapping("/hasKey")
    public Boolean hasKey(String key) {
        Boolean aBoolean = redisUtil.hasKey(key);
        return aBoolean;
    }

    @GetMapping("/setTime")
    public void setTime(String key,Object value,Long expires) {
        redisUtil.set(key,value,expires, TimeUnit.SECONDS);
    }

    @PostMapping("/add")
    public void add(String key,long delt) {
        long incr = redisUtil.incr(key, delt);
        if(incr > 0) {
            Object o = redisUtil.get(key);
            System.out.println(JSON.toJSONString(o));
        }
    }

    @GetMapping("/decr")
    public void decr(String key,long delt) {
        long decr = redisUtil.decr(key,delt);
        if (decr > 0) {
            Object o = redisUtil.get(key);
            System.out.println(JSON.toJSONString(o));
        }
    }

    @GetMapping("/hset")
    public Boolean hset(String key,String hkey,Object value) {
        User user = new User();
        user.setName("liang");
        user.setAge(20);
        Boolean hset = redisUtil.hset(key, hkey, JSON.toJSONString(user));
        System.out.println(hset);
        return hset;
    }

    @GetMapping("/hget")
    public Object hget(String key,String hkey) {
        Object result = redisUtil.hget(key,hkey);
        System.out.println(result);
        return result;
    }

    @GetMapping("/hdel")
    public void hdel(String key,String... hkey) {
        redisUtil.hdel(key,hkey);
    }

    @PostMapping("/hHasKey")
    public boolean HhasKey(String key, String hkey) {
        boolean flag = redisUtil.hHasKey(key, hkey);
        return flag;
    }

    @PostMapping("/sget")
    public Set sget(String key) {
        Set<Object> objects = redisUtil.sGet(key);
        Iterator iterator = objects.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        return objects;
    }

    @PostMapping("/sset")
    public void sSet(String key,String... values) {
        long l = redisUtil.sSet(key, values);
        System.out.println(l);
    }

    @PostMapping("/setAndTime")
    public void setAndTime(String key,String... values) {
        long l = redisUtil.sSetAndTime(key, 60,values);
        System.out.println(l);
    }

    @PostMapping("/setRemove")
    public void setRemove(String key,String... values) {
        long sports = redisUtil.setRemove(key, values);
        System.out.println(sports);
    }


    @PostMapping("/lget")
    public List<Object> lGet(String key) {
        List<Object> objects = redisUtil.lGet(key);
        for (Object object:objects) {
            System.out.println(object);
        }
        return objects;
    }

    @PostMapping("/lpush")
    public void lPush(String key,String values) {
        long lpush = redisUtil.lpush(key, values);
        log.info("lget:{}",lpush);
    }

    @PostMapping(value = "/lPushList",produces = "application/json")
    public void lPushList(String key,@RequestBody List<Object> values) {
        log.info("values:{}",values);
        long lpush = redisUtil.lpush(key, values);
        List<Object> lists = redisUtil.lGet(key);
        log.info("key:{}",lists);
    }

    @PostMapping("/rpush")
    public void rPush(String key,String value) {
        log.info("key:{},value:{}",key,value);
        long result = redisUtil.rpush(key, value);
        log.info("result:{}",result);
    }

    @PostMapping("/rpushAndTime")
    public void rPushAndTime(String key,String value,long time) {
        log.info("------------------------");
        boolean rpush = redisUtil.rpush(key, value, time);
        log.info("rpush:{}",rpush);
    }

    @PostMapping("/lremove")
    public boolean lRemove(String name,int count,String value) {
        long result = redisUtil.lRemove(name, count, value);
        return true;
    }
}
